<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .active {
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <input v-model="student" @keyup.enter="addStudent" type="text" placeholder="输入内容回车，就可以添加" />
        </div>
        <div v-if="students.length==0">这个班级还没有学生</div>
        <div v-else>
            <ul>
                <!-- :class="{active:selectedStudent==name}：设置一个类，当selectedStudent==name的时候生效 -->
                <!-- @click="selectedStudent=name：当li被单击的时候，把name赋值给selectedStudent -->
                <li v-for="name in students" :key="name" :class="{active:selectedStudent==name}"
                    @click="selectedStudent=name">
                    {{ name }}
                </li>
            </ul>
        </div>
    </div>
</body>
<script src="../assets/vue2.js"></script>
<script>
    const app = new Vue({
        el: '#app',
        data() {
            return {
                student: '', // v-model的变量
                selectedStudent: '',// 初始化
                students: ['张三', '李四', '王二', '赵五', '老六']
            }
        },
        methods: {
            addStudent() {
                this.students.push(this.student);//新增，记得写this
                this.student = '';//清空
            }
        },
    })
</script>

</html>